package cn.com.cetccst.appstore.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import cn.com.cetccst.appstore.R;
import cn.com.cetccst.appstore.databinding.ActivityCategoryAppListBinding;
import cn.com.cetccst.appstore.ui.fragment.AppListFragment;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;

public class CategoryAppListActivity extends BaseActivity<ActivityCategoryAppListBinding> {

    public static final String EXTRA_CATEGORY_ID = "category_id";
    public static final String EXTRA_CATEGORY_NAME = "category_name";


    @Override
    protected void initViewModel() {

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_category_app_list, 0, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setSupportActionBar(getBinding().toolBar);
        getSupportActionBar().setTitle(getIntent().getStringExtra(EXTRA_CATEGORY_NAME));

        Fragment fragment = new AppListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppListFragment.EXTRA_CATEGORY_ID, getIntent().getStringExtra(EXTRA_CATEGORY_ID));
        fragment.setArguments(bundle);
        showFragment(fragment);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.appstore_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.search) {
            Intent intent = new Intent(this, SearchActivity.class);
            intent.putExtra(SearchActivity.EXTRA_CATEGORY_ID, getIntent().getStringExtra(EXTRA_CATEGORY_ID));
            intent.putExtra(SearchActivity.EXTRA_CATEGORY_NAME, getIntent().getStringExtra(EXTRA_CATEGORY_NAME));
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_container, fragment).commitNowAllowingStateLoss();
    }

}
